# 此阶段下载本次构建必要文件
FROM alpine:latest as glibc.download

# ARG GLIBC_URL="https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-2.35-r0.apk"
ARG SGERRAND_RUL="https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub"
# ARG GLIBC_BIN="https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-bin-2.35-r0.apk"
# ARG GLIBC_I18N="https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-i18n-2.35-r0.apk"

ARG GLIBC_URL="https://gitee.com/lizhip/alpine-pkg-glibc/attach_files/1031099/download/glibc-2.35-r0.apk"
ARG GLIBC_BIN="https://gitee.com/lizhip/alpine-pkg-glibc/attach_files/1031100/download/glibc-bin-2.35-r0.apk"
ARG GLIBC_I18N="https://gitee.com/lizhip/alpine-pkg-glibc/attach_files/1031101/download/glibc-i18n-2.35-r0.apk"

RUN set -x \
&& sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk --no-cache add ca-certificates tzdata wget busybox-extras \
&& mkdir /build \
&& cd /build \
&& wget -c -q -O /etc/apk/keys/sgerrand.rsa.pub ${SGERRAND_RUL} \
&& wget -c ${GLIBC_URL} && wget -c ${GLIBC_BIN} && wget -c ${GLIBC_I18N}


# 此阶段是最终需求的构建(得到一个包含中文语言包、中国标准时区的alpine-glibc镜像)
FROM alpine:latest

COPY --from=glibc.download /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY --from=glibc.download /etc/apk/keys/sgerrand.rsa.pub /etc/apk/keys/sgerrand.rsa.pub
COPY --from=glibc.download /build /build
COPY --from=glibc.download /bin/busybox-extras /bin/busybox-extras
COPY --from=glibc.download /bin/busybox /bin/busybox

RUN set -x \
&& sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& cd /build \
&& apk add glibc-2.35-r0.apk \
&& apk add glibc-bin-2.35-r0.apk \
&& apk add glibc-i18n-2.35-r0.apk \
&& gunzip /usr/glibc-compat/share/i18n/charmaps/UTF-8.gz \
&& ldconfig /lib /usr/glibc-compat/lib \
&& /usr/glibc-compat/bin/localedef -i zh_CN -f UTF-8 zh_CN.UTF-8 \
&& rm -rf /build \
&& apk del glibc-i18n \
&& rm -rf /var/cache/apk/*

ENV PATH="/usr/glibc-compat/bin:$PATH" \
    LANG="zh_CN.UTF-8"